package com.example.prolyric;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;


import com.example.prolyric.songmanager.Song;
import com.example.prolyric.songmanager.SongsManager;

public class ListLibrary extends ListActivity {
	
	private ArrayList<String> listAlbum ;
	private ArrayList<Song> listAllSong ;
	private ArrayList<String> playlists;
	private ArrayList<Song> listTopRate;
	
	
	private static final String [] TYPE_SELECT_STRING = 
		{"All Song", 
		 "Playlist",
		 "Top Rate",
		 "Album",
		 "Artist",
		 "Gener"};
	private static final String[] NO_PLAYLIST = {"Playlist is empty"};
	private static final int[] TYPE_SELECT_INT = {0,1,2,3,4,5};
	private  int [] deep = {-1,-1,-1};
	private int current_deep = 0;
	
	
	
	
	private SongsManager songManager;
	
	private Button btn_back ;
	private Button btn_playlist_manager;
	

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_library);
        
        
       
        btn_back = (Button)findViewById(R.id.btn_library_back);
        btn_playlist_manager = (Button)findViewById(R.id.btn_playlist_manager);
     
        songManager = new SongsManager(this);
       
        playlists = songManager.getAllPlaylists();
        listAlbum = songManager.getAllAlbum();
   	 listAllSong = songManager.getAllSong();
   	 listTopRate = songManager.getTopRate();
   	 
        setList(TYPE_SELECT_STRING);
       
        btn_back.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				ArrayList<String> listAlbum = songManager.getAllAlbum();
				if(current_deep == 2)
				{
					if(deep[0] == SongsManager.ALBUMS)
		    		{
		    			
		    			String [] listAlbumArr = new String[listAlbum.size()];
		    			listAlbumArr = listAlbum.toArray(listAlbumArr);
		    			
		    			setList(listAlbumArr);
		    			
		    			 current_deep = 1;
		    		}
					
					else if(deep[0] == SongsManager.ALL_SONG)
					{
						setList(TYPE_SELECT_STRING);
						current_deep = 0;
					}
				}
				else if(current_deep == 1)
				{
					setList(TYPE_SELECT_STRING);
					current_deep = 0;
				}
				
			}
		});
        
        
        /*
         * Button PlaylistManager 
         * chuyen sang activity PlaylistManager
         * */
        
        
        btn_playlist_manager.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent i = new Intent(ListLibrary.this, PlaylistManagerActivity.class);
				startActivity(i);
			}
		});
       
		
    }
    
    public void setList(String[] array)
    {
    	this.setListAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, android.R.id.text1,array));
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	
    	
    	/*
    	 * Current_deeep == 0 man hinh dau tien, chon the loai phat
    	 * */
    	if(current_deep == 0)
    	{
    		deep[0] = position;
    		deep[0] = TYPE_SELECT_INT[position];
    		if(deep[0] == SongsManager.ALBUMS)
    		{
    			
    			String [] listAlbumArr = new String[listAlbum.size()];
    			listAlbumArr = listAlbum.toArray(listAlbumArr);
    			 setList(listAlbumArr);
    			 current_deep = 1;
    		}
    		
    		else if(deep[0] == SongsManager.ALL_SONG)
    		{
    			
        		String[] songnames = new String[listAllSong.size()];
        		for(int i = 0 ; i< listAllSong.size(); i++)
        		{
        			songnames[i] = listAllSong.get(i).getSong_display_name();
        			
        		}
        		setList(songnames);
    			 current_deep = 2;
    			 
    		}
    		
    		else if(deep[0] == SongsManager.PLAY_LIST)
    		{
    			btn_playlist_manager.setText("Playlist Manager");
    			btn_playlist_manager.setVisibility(View.VISIBLE);
    			
    			if(playlists == null)
    			{
    				setList(NO_PLAYLIST);
    			}
    			
    			else
    			{
    				//Log.d("so luong playlist", String.valueOf(playlists.size()));
    				String [] playlistStr = new String[playlists.size()];
    				playlistStr = playlists.toArray(playlistStr);
    				for(int i = 0 ;i < playlistStr.length;i++)
    				{
    					playlistStr[i] = playlistStr[i].substring(0,playlistStr[i].length()-4);
    				}
    				setList(playlistStr);
    			}
    			 current_deep = 1;
    		}
    		
    		else if(deep[0] == SongsManager.TOP_RATE)
    		{
    			String[] songnames = new String[listTopRate.size()];
        		for(int i = 0 ; i< listTopRate.size(); i++)
        		{
        			songnames[i] = listTopRate.get(i).getSong_display_name();
        			
        		}
        		setList(songnames);
    			 current_deep = 2;
    		}
    	}
    	
    	
    	/*
    	 * Current_deep = 1 : chon ten cua the loai phat, neu la all song thi chon ten bai hat
    	 * */
    	
    	else if(current_deep == 1)
    	{
    		deep[1] = position;
    		if(deep[0] == SongsManager.ALBUMS)
    		{
    			ArrayList<Song> lisSongs = songManager.getSongsByAlbum(listAlbum.get(deep[1]));
        		String[] songnames = new String[lisSongs.size()];
        		for(int i = 0 ; i< lisSongs.size(); i++)
        		{
        			songnames[i] = lisSongs.get(i).getSong_display_name();
        			
        		}
        		setList(songnames);
    		}
    		else if(deep[0] == SongsManager.ALL_SONG)
    		{
    			
    		}
    		else if(deep[0] == SongsManager.PLAY_LIST)
    		{
    			//Toast.makeText(getBaseContext(), "playlist name : " + playlists.get(deep[1]), Toast.LENGTH_LONG).show();
    			ArrayList<Song> lisSongs = songManager.readContentOfPlaylist(playlists.get(deep[1]));
    			if(lisSongs == null)
    			{
    				Toast.makeText(getBaseContext(), "playlist is empty ", Toast.LENGTH_LONG).show();
    			}
    			else 
    			{
    				String[] songnames = new String[lisSongs.size()];
            		for(int i = 0 ; i< lisSongs.size(); i++)
            		{
            			songnames[i] = lisSongs.get(i).getSong_display_name();
            			
            		}
            		setList(songnames);
    			}
    			//boolean del = songManager.deletePlaylist(playlists.get(deep[1]));
    			//Toast.makeText(getBaseContext(), String.valueOf(del), Toast.LENGTH_LONG).show();
    			
    		}
			 current_deep = 2;
    		
    		
    	}
    	
    	//chon bai hat de phat
    	else if(current_deep == 2)
    	{
    		
    		    		//Toast.makeText(getBaseContext(), "ten bai hat : " + song.getSong_display_name(), Toast.LENGTH_LONG).show();
    		//String audioFilePath = song.getAudioFilePath();
    		//String mimeType = song.getSong_mine_type();
    		//SharedPreferences playlistShare = getSharedPreferences("playlist", MODE_PRIVATE);
    		//SharedPreferences.Editor editor = playlistShare.edit();
    		//editor.
    		Log.v("deep", "type= "+deep[0]+"name = "+deep[1]+"position="+position);
    		Intent data = new Intent();
    		data.setData(Uri.parse("result"));
    		data.putExtra("type", deep[0]);
    		if(deep[0]== SongsManager.ALBUMS)
    			data.putExtra("name", listAlbum.get(deep[1]));
    		else if(deep[0] == SongsManager.ALL_SONG)
    		{
    			data.putExtra("name","All_song");
    		}
    		
    		else if(deep[0]== SongsManager.PLAY_LIST)
    		{
    			data.putExtra("name", playlists.get(deep[1]));
    		}
    		else if(deep[0] == SongsManager.TOP_RATE)
    		{
    			data.putExtra("name", "Top_rate");
    		}
    		data.putExtra("position", position);
    		setResult(RESULT_OK, data);
    		finish();
			
    	}
    }
     
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_list_library, menu);
        return true;
    }
}
